package com.xhwl.logistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhwl.logistics.bo.VehicleParkingSpaceBO;
import com.xhwl.logistics.bo.cent.VehicleIntegratedConfigBO;
import com.xhwl.logistics.component.SignForCoSon;
import com.xhwl.logistics.dao.mapper.IVehicleParkingSpaceMapper;
import com.xhwl.logistics.dto.push.CarSpaceDataCoSonDTO;
import com.xhwl.logistics.entity.VehicleParkingSpace;
import com.xhwl.logistics.service.IVehicleParkingSpaceService;
import com.xhwl.logistics.service.cent.QueryDeviceService;
import org.springframework.beans.factory.annotation.Autowired;


//@Service
public class VehicleParkingSpaceServiceImpl extends ServiceImpl<IVehicleParkingSpaceMapper, VehicleParkingSpace> implements IVehicleParkingSpaceService {

    @Autowired
    private IVehicleParkingSpaceMapper iVehicleParkingSpaceMapper;
    @Autowired
    private QueryDeviceService queryDeviceService;
    @Autowired
    private SignForCoSon signForCoSon;

    @Override
    public VehicleParkingSpaceBO getCountForSpace(Integer projectId) {
        VehicleParkingSpace vehicleParkingSpace = iVehicleParkingSpaceMapper.selectOne(new QueryWrapper<VehicleParkingSpace>().eq("project_id",projectId));
        VehicleParkingSpaceBO vehicleParkingSpaceBO = new VehicleParkingSpaceBO();
        vehicleParkingSpaceBO.setProjectId(projectId);
        vehicleParkingSpaceBO.setParkingLotName(vehicleParkingSpace.getParkingLotName());
        vehicleParkingSpaceBO.setCarportTotalNumber(vehicleParkingSpace.getCarportTotalNumber());
        vehicleParkingSpaceBO.setCarportFreeNumber(vehicleParkingSpace.getCarportFreeNumber());
        vehicleParkingSpaceBO.setCarportExistNumber(vehicleParkingSpace.getCarportExistNumber());
        return vehicleParkingSpaceBO;
    }
    @Override
    public VehicleIntegratedConfigBO sendparkSpace(CarSpaceDataCoSonDTO carSpaceDataCoSonDTO) {
        VehicleParkingSpace vehicleParkingSpace = new VehicleParkingSpace();
        vehicleParkingSpace.setParkId(carSpaceDataCoSonDTO.getKey());
        vehicleParkingSpace.setCarportTotalNumber(Integer.valueOf(carSpaceDataCoSonDTO.getTotal_spaces()));
        vehicleParkingSpace.setCarportFreeNumber(Integer.valueOf(carSpaceDataCoSonDTO.getRemainder_spaces()));
        vehicleParkingSpace.setCarportExistNumber(vehicleParkingSpace.getCarportTotalNumber()- vehicleParkingSpace.getCarportFreeNumber());
        VehicleIntegratedConfigBO vehicleIntegratedConfigBO = queryDeviceService.queryIntegratedConfigFromParkId(vehicleParkingSpace.getParkId());
        Integer ProjectId = vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProjectId();
        vehicleParkingSpace.setParkingLotName(vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getRemark());
        vehicleParkingSpace.setProjectId(ProjectId);
        VehicleParkingSpace vehicleParkingSpace1 = iVehicleParkingSpaceMapper.selectOne(new QueryWrapper<VehicleParkingSpace>().eq("project_id",ProjectId));
        if(vehicleParkingSpace1.getParkId() == null && vehicleParkingSpace1.getParkingLotName() == null){
            iVehicleParkingSpaceMapper.insert(vehicleParkingSpace);
        }
        else{
            vehicleParkingSpace.setId(vehicleParkingSpace1.getId());
            iVehicleParkingSpaceMapper.updateById(vehicleParkingSpace);
        }
        return vehicleIntegratedConfigBO;
    }
}
